package cn.shenlong.front.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.shenlong.common.Page;
import cn.shenlong.front.directive.abs.AbstractChannelDirective;
import cn.shenlong.portal.domain.Discount;
import cn.shenlong.portal.service.DiscountService;
import cn.shenlong.util.DirectiveUtils;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * 商家优惠信息标签
 * 
 * @author shenl
 * 
 */
@Component("cms_discount_list")
public class DiscountDirective extends AbstractChannelDirective {

	@Autowired
	private DiscountService discountService;


	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Long storeId = DirectiveUtils.getLong("storeId", params);
		List<Discount> list;
		if (storeId != null) {
			Map<String,Object> para = new HashMap<String, Object>();
			para.put("storeId", storeId);
			Page<Discount> page=discountService.findPageListByCriteria(null, null, para);
			if(page!=null){
				list = (List<Discount>) page.getList();
				env.setVariable(DirectiveUtils.OUT_LIST, DEFAULT_WRAPPER.wrap(list));
			}
		} 
		body.render(env.getOut());
	}
}
